<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="js/bootstrap.min.js"></script>

<?php 

error_reporting(E_ALL);

include('functions.php');
dbConnect();



if(isset($_SESSION['user'])){
		$loggedIn = TRUE;
} else {
		$loggedIn = FALSE;
}

$ip = $_SERVER['REMOTE_ADDR'];

date_default_timezone_set('America/Denver');
$today = date("y-m-d");
/*
$ipQuery = mysql_query("SELECT * FROM ipLog WHERE (ip = '$ip' AND date = '$today')");

if(mysql_num_rows($ipQuery) > 0){
	mysql_query("UPDATE ipLog SET visits = visits +1 WHERE (ip = '$ip' AND date = '$today')");
} else {
	mysql_query("INSERT INTO ipLog (ip, status, visits, date, failed_logons) VALUES ('$ip', 'active', 1, '$today', 0)");
}


if(isset($_COOKIE['firstVisit'])){
	//do nothing
} else {
	setcookie("firstVisit", "firstVisit", time()+3600);
}
*/

$liUser = liUser();
$liDomain = liDomain();
$liId = liId();
$liLevel = liLevel();;



$banned = false; //Create the banner system

if($banned){
	echo 'you have been banned, sorry';
	
} else {
	
	//URL disection
	if(empty($_GET)){
		$college = 'all'; 
		$sort = 'notSet';
		$search = 'empty';
		$item = 'empty';
	} else if(isset($_GET['item'])){
		$college = 'all'; 
		$sort = 'notSet';
		$search = 'empty';
		$item = $_GET['item'];
	} else if(isset($_GET['college']) || isset($_GET['sort']) || isset($_GET['search'])){
		
		$item = 'empty';
		
		if(isset($_GET['college'])){
			$college = $_GET['college'];
		}
		
		if(isset($_GET['search'])){
			$search = $_GET['search'];
		}
	
		if(!isset($_GET['college']) or $_GET['college'] == null){
			$college = 'all';
		}
				
		if(!isset($_GET['sort'])){
			$sort = 'notSet';
		} else {
			$sort = $_GET['sort'];
		}
		
		if(!isset($_GET['search']) or $_GET['search'] == null){
			$search = 'empty';
		}
	}
	
	$pages = 0;

	
	
	
	//SQL selection
	if($college!='all'){
		if($sort=='notSet'){
			if($search=='empty'){
				$sql = "SELECT * FROM posts WHERE domain = '$college'";
			} else {
				$sql = "SELECT * FROM posts WHERE domain = '$college' AND item LIKE '%$search%' OR description LIKE '%$search%'";
			}
			
		} else {
			if($sort=='priceLowHigh'){
				$sql = "SELECT * FROM posts WHERE domain = '$college' ORDER BY price ASC";
			} else if($sort=='priceHighLow'){
				$sql = "SELECT * FROM posts WHERE domain = '$college' ORDER BY price DESC";
			} else if($sort=='hitsAsc'){
				$sql = "SELECT * FROM posts WHERE domain = '$college' ORDER BY hits ASC";
			} else if($sort=='hitsDesc'){
				$sql = "SELECT * FROM posts WHERE domain = '$college' ORDER BY hits DESC";
			}

		}
	} elseif($item!='empty'){
		$sql = "SELECT * FROM posts WHERE id = '$item'";
	} elseif($search!='empty'){
		$sql = "SELECT * FROM posts WHERE item LIKE '%$search%' OR description LIKE '%$search%'";
	} else {
		$sql = 'SELECT * FROM posts ORDER BY created ASC';		
	}
	
	$queryFull = mysql_query("$sql");
	$limitSql = $sql . ' LIMIT 0, 40';
	$query = mysql_query("$limitSql");
	
	//Declare ajax loads based on query count
	$rowCount = mysql_num_rows($queryFull);
	
	$pages = ceil((($rowCount - 40) / 10));

		
	include('./head.php');
	
	include('./analytics.php');

	
	if(isLi()){
		echo '<div class="span12 top">';
		
		?> <a href="#" onclick="Effect.toggle('postSomething', 'blind'); return false;">
			<i class="icon-tags icon-white"></i>
			<b>Post An Item</b>
			</a>
			
	<?php
		
		echo '<div id="postSomething" style="display:none">';
		echo '<table border="0"><tr><td>
			<form name="post" onsubmit="return validate()" action="postItem.php" method="post">
				<input type="text" name="name" value="Item Name" /><br />
				<input type="text" name="price" value="Price $$" /><br />
				<textarea name="description" rows="6" cols="100">Item Description, 2300 characters maximum</textarea><br />
				<b>Picture:</b> <input type="file" name="file" id="file" /><br />
				<input type="checkbox" name="disclaimer" />I have read and agree to the terms of use (to the right)<br />
				
				<input type="hidden" name="postItem" value="TRUE">
				<input type="hidden" name="user" value="' . liUser() . '">
				<input type="hidden" name="domain" value="' . liDomain() . '">
				<input type="submit" value="Post Item" />
			</form>
		';
		echo '</td><td>';
		
		echo '<iframe src="postDisclaimer.php" frameborder="0" width="450" height="135">';
		echo '	  <p>Your browser does not support iframes.</p>';
		echo '</iframe>';
		
		echo '</td></tr></table></div>';
	}
	
	echo '<div class="span12 top">';
	
	echo '<table border="0"><td>';
		
	$collegeQuery = mysql_query("SELECT domain, name FROM domains");
	
	echo '<td><form name="domainForm">';
	
		
		$selectName = '<select name="domain" OnChange="location.href=domainForm.domain.options[selectedIndex].value">';
	
		echo $selectName;
		
			echo '<option value="">Select a College</option>';
			echo '<option value="?search=' . $search . '&?college=all">All Colleges</option>';
			
			$counter = 1;
			while($row = mysql_fetch_assoc($collegeQuery)){
				
				if($row['domain']==$college){
					$selected = 'SELECTED';
				} else {
					$selected = '';
				}
				
				echo '<option ' . $selected . ' value="' . getUrl() . '?search=' . $search . '&college=' . $row['domain'] . '">';
				echo $row['name'] . ' (' . $row['domain']  . ')</option>';
				
				$counter++;
			}
			
		echo '</select>';
	echo '</form>';
		
	echo'</td><td>';
	
	
	
	if($college!='all'){
		
		if($sort=='priceLowHigh'){
			$lowHigh = 'SELECTED';
		} else {
			$lowHigh = '';
		}
		
		if($sort=='priceHighLow'){
			$highLow = 'SELECTED';
		} else {
			$highLow = '';
		}
		
		if($sort=='hitsAsc'){
			$hitsAsc = 'SELECTED';
		} else {
			$hitsAsc = '';
		}
		
		if($sort=='hitsDesc'){
			$hitsDesc = 'SELECTED';
		} else {
			$hitsDesc = '';
		}
		
		echo '<form name="sortForm">';
		echo '<select name="sort" OnChange="location.href=sortForm.sort.options[selectedIndex].value">';
				echo '<option value="">Sort by..</option>';
				echo '<option ' . $lowHigh . ' value="' . getUrl() . '?search=' . $search . '&college=' . $college . '&sort=priceLowHigh">Price Low to High</option>';
				echo '<option ' . $highLow . ' value="' . getUrl() . '?search=' . $search . '&college=' . $college . '&sort=priceHighLow">Price High to Low</option>';
				echo '<option ' . $hitsAsc . ' value="' . getUrl() . '?search=' . $search . '&college=' . $college . '&sort=hitsAsc">Likes Low to High</option>';
				echo '<option ' . $hitsDesc . ' value="' . getUrl() . '?search=' . $search . '&college=' . $college . '&sort=hitsDesc">Likes High to Low</option>';
			echo '</select>';
		echo '</form>';
	}
	
	echo '</td><td style="padding-bottom:10px;">';
	
	echo '<form name="searchForm" class="form-search" action="index.php" method="get">';
	echo '<div class="input-append">';
		echo '<input type="text" class="span2 search-query" name="search" value="search">';
		echo '<input type="hidden" name="college" value="' . $college . '">';	
		echo '<input type="submit" class="btn" value="Search" />';
	echo '</div>';
	

	
	if($search!='empty'){
		echo '  <b>Search "' . $search . '</b>"';
		echo  '<a href="' . getUrl() . '?college=' . $college . '"> <small>(clear search)</small>';
	}
		echo '</form>';
	echo '</td>';
	
	echo '</table></div>';
?>	

<div class="container-fluid">
	<div style="padding-right:15px; padding-left:15px;" class="span12">
	
<?php
	echo '<div id="postswrapper">';

	if($rowCount<1){
		if($search!='empty'){
		echo '<center><b>Sorry, we could not find anything in that matched your search <i>' . $search . '</i></b></center>';
		} else {
			echo '<center><b> Sorry we could not find any posts from ' . getCollegeName($college) . ', try <a href="' . getUrl() . '">from the beginning</a></b></center>';
		} 
	}

		$switch = 1; 
		$count = 0;
		while($row = mysql_fetch_array($query)){
			$switch = 1; 
			echo '<div style="-moz-border-radius: 7px;border-radius: 7px;margin-bottom:0px;padding-top:3px;padding-bottom:.5px;border:1px solid #9CAA9C;margin-bottom:4px;height:20px" id="result">'; 

			include('post.php');

			if($count <9){
				$count++;
			} else {
				echo '';
				$count = 0;
			}
		}
		
		echo '</div><div id="loadmoreajaxloader" style="display:none;"><center><img src="./img/ajax-loader.gif" /></center></div>';
		
		echo '</div></div>';
		
		
		include('foot.php');
}

mysql_close();

 ?>
